<template>
  <el-dialog title="添加电影" :visible="filmFormVisible" :before-close="closeAddFilmForm">
    <el-form :model="form" ref="formFilm" status-icon :rules="createRules()">
      <el-form-item label="电影名称" label-width="120px" prop="name">
        <el-input v-model="form.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="电影封面" label-width="120px" prop="poster">
        <!-- 
          action:它指向的是你服务器端文件上传的请求地址
          multiple:允许上传多张
         -->
        <el-upload
          class="upload-film"
          action="http://localhost:9000/api/upload"
          :headers="{ token: $store.getters.token }"
          :multiple="false"
          :on-success="onSuccess"
        >
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
        </el-upload>
      </el-form-item>
      <el-form-item label="导演" label-width="120px" prop="director">
        <el-input v-model="form.director" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="电影分类" label-width="120px" prop="category">
        <el-select v-model="form.category" placeholder="请选择对应的分类">
          <el-option label="动画|冒险" value="动画|冒险"></el-option>
          <el-option label="动作|喜剧" value="动作|喜剧"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="地区" label-width="120px" prop="nation">
        <el-input v-model="form.nation" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="上映时间" label-width="120px">
        <el-date-picker v-model="form.premiereAt" type="date" placeholder="选择日期" />
      </el-form-item>
      <el-form-item label="电影时长" label-width="120px">
        <el-input v-model.number="form.runtime" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="电影简介" label-width="120px" prop="synopsis">
        <el-input type="textarea" v-model.number="form.synopsis" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="电影描述" label-width="120px">
        <!-- 富文本封装成一个组件，方便在项目中调用 -->
        <MyEditor ref="editor" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeAddFilmForm">取 消</el-button>
      <el-button :loading="loading" type="primary" @click="addFilmForm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import moment from 'moment'
import { addNowPlayingFilm } from '@/api/filmApi'
import MyEditor from '@/components/MyEditor'

export default {
  props: ['filmFormVisible'],
  components: {
    MyEditor
  },
  data() {
    return {
      loading: false,
      form: {
        name: '',
        poster: '',
        director: '',
        category: '',
        nation: '本地',
        premiereAt: new Date(Date.now() + 86400 * 7000),
        runtime: 90,
        synopsis: '',
        info: ''
      }
    }
  },
  mounted() {
    // 如果没有获取到子组件中的ref对象则不进行处理
    this.$refs.editor && (this.$refs.editor.html = '')
  },
  methods: {
    onSuccess(response) {
      this.form.poster = response.data.url
    },
    closeAddFilmForm() {
      this.$emit('onFilmFormVisible', false)
    },
    addFilmForm() {
      // 点击添加按钮后有加载等待的效果，防多次触发
      this.loading = true
      this.$refs.formFilm.validate(async valid => {
        if (valid) {
          // 验证通过
          let postData = {
            ...this.form,
            premiereAt: moment(this.form.premiereAt).format('YYYY-MM-DD'),
            info: this.$refs.editor.html
          }
          let ret = await addNowPlayingFilm(postData)
          if (ret.code == 0) {
            this.$message({
              type: 'success',
              message: '添加电影成功',
              duration: 1500,
              onClose: () => {
                this.form = {
                  name: '',
                  director: '',
                  category: '',
                  nation: '本地',
                  premiereAt: new Date(Date.now() + 86400 * 7000),
                  runtime: 90,
                  synopsis: ''
                }
                this.loading = false
                this.$emit('onFilmFormVisible', false)
              }
            })
          }
        } else {
          // 验证不通过
          this.$message({
            type: 'error',
            message: '验证不通过',
            duration: 1500,
            onClose: () => (this.loading = false)
          })
        }
      })
    },
    createRules() {
      return {
        name: [{ required: true, message: '必须填写', trigger: 'blur' }]
        // director: '',
        // category: '',
        // nation: '',
        // premiereAt: new Date(Date.now() + 86400 * 7000),
        // runtime: 90,
        // synopsis: ''
      }
    }
  }
}
</script>

<style lang="scss" scoped></style>
